﻿@inherits TscComponentBase

<div class="@Class full-height">
    <MCard Class="d-flex flex-column rounded-lg full-height">
        <PanelTooltip Title="@Value.Title" Description="@Value.Description" />
        <AutoResize Delay="10" OnResize="ResizeAsync">
            <MCardText Class="flex-grow-1 full-height">
                @{
                    var options = Value.GetChartOption(CurrentTimeZone);
                    if (options is null)
                    {
                        <NoData />
                    }
                    else
                    {
                        <MECharts Option="options" @ref=Chart />
                    }
                }
            </MCardText>
        </AutoResize>
    </MCard>
</div>

@code {
    [Parameter]
    public IEChartPanelValue Value { get; set; }

    public MECharts? Chart { get; set; }

    public string? OldKey { get; set; }

    public async Task ResizeAsync()
    {
        if (Chart is not null)
        {
            await Chart.Resize();
        }
    }

    protected override async Task OnParametersSetAsync()
    {
        var key = Value.GetChartKey();
        if (OldKey != key && Chart is not null)
        {
            OldKey = key;
            await Chart.ReinitializeECharts();
        }
    }
}
